//
//  HQCSpecificViewController.m
//  NursingManages
//
//  Created by Zhen Huang on 17/6/1.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HQCSpecificViewController.h"
#import "SizeToFit.h"
#import "HCommon.h"
#import <TMCache.h>
#import "HQCDepartViewController.h"
#import "MPickerSelectionView.h"
#import "UserDefault.h"
#import "Model_Pub_Employee.h"
#import "HQc_codeModel.h"
#import "HQCSpecificTableViewCell.h"
#import "Model_Hr_InspectDetail_New.h"
#import "Model_Hr_Ns_Inspect_New.h"
#import "HQualitySave.h"
#import "HQualityViewController.h"
#import "InitDbTable.h"
#import "Model_Hr_Qc_Spec.h"
#import "DQFormViewController.h"
#import "InitDbTableQuality.h"
#import "HQualityDetailViewController.h"
#import "HQPickerView.h"
#import "HQChooseReasonViewController.h"
@interface HQCSpecificViewController ()
<UITextFieldDelegate,
UITableViewDelegate,
UITableViewDataSource,
MPickerSelectionViewProtocol,
UITextViewDelegate,
HQPickerViewDelegate>

@property (nonatomic ,strong) UIButton *CloseButton;
@property (nonatomic ,strong) UIButton *SaveButton;
@property (nonatomic ,strong) HQc_codeModel *model;
@property (nonatomic ,strong) UIButton *departButton;
@property (nonatomic ,strong) UIButton *checkPeopleButton;
@property (nonatomic ,strong) UILabel *OtherCheckPeopleLabel;
@property (nonatomic ,assign) NSInteger section;
@property (nonatomic ,strong) NSMutableArray *arrays;
@property (nonatomic ,strong) NSMutableArray *dept_nameArray;
@property (nonatomic ,strong) NSMutableArray *dept_idArray;
@property (nonatomic ,strong) NSArray *dataArray;
@property (nonatomic ,strong) UITableView *tableview;
@property (nonatomic ,strong) UIButton *dateTimeButton;
@property (nonatomic ,strong) UILabel *dateTimeLabel;
@property (nonatomic ,strong) UIDatePicker *datePicker;
@property (nonatomic ,strong) Model_Hr_Ns_Inspect_New *inspectModel;
@property (nonatomic ,strong) MPickerSelectionView *pickerSelectionView;
@property (nonatomic ,strong) NSMutableArray *bigArray;
@property (nonatomic ,strong) NSString *level_id;
@property (nonatomic ,assign) NSInteger cellHeight;

@property (nonatomic ,strong) NSArray *array;
@property (nonatomic ,strong) NSArray *itemArray;
@property (nonatomic ,strong) NSArray *resultArray;
@property (nonatomic ,strong) NSArray *reasonIDArray;
@property (nonatomic ,assign) BOOL isCheck;

@property (nonatomic ,assign) BOOL isHasPC;
@end

@implementation HQCSpecificViewController

- (instancetype)initWithSpecificMoel:(HQc_codeModel *)model
                             Section:(NSInteger)section
                           dataArray:(NSArray *)dataArray
                            level_id:(NSString *)level_id
{
    if (self) {
        self.model = model;
        self.section = section;
        self.arrays = [NSMutableArray new];
        self.dataArray = dataArray;
        self.dept_nameArray = [NSMutableArray new];
        self.bigArray = [NSMutableArray new];
        self.dept_idArray = [NSMutableArray new];
        self.level_id = level_id;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableview];
    [self assigmentData];
    
    NSMutableArray *paramArray = [NSMutableArray new];
    paramArray = [[InitDbTable alloc]getParam];
    
    if (paramArray.count != 0) {
        
        Model_Hr_Param *model = paramArray[0];
        
        if ([model.PARAM_VAL isEqualToString:@"1"]) {
            
            self.isHasPC = 1;
        }

    }
    
    InitDbTableQuality *initDb = [[InitDbTableQuality alloc]init];
    
    self.resultArray = [initDb GetDetailInspectTableWithResultAndPar_id:self.result_id par_id:self.par_id];
    
    if (self.resultArray.count > 0) {
        
        self.isCheck = 1;
        for (int i = 0; i < self.resultArray.count; i++) {
            Model_Hr_InspectDetail_New *detailModel = self.resultArray[i];
            Model_Hr_InspectDetail_New *detailModel1 = self.bigArray[i];
            detailModel1.SCORE = detailModel.SCORE;
            detailModel1.RESION = detailModel.RESION;
            detailModel1.RESION_ID = detailModel.RESION_ID;
            HQc_codeModel *model = self.dataArray[i];
            model.point = detailModel.SCORE;
            model.SCORE = [NSString stringWithFormat:@"%.1f",detailModel.SCORE];
            model.reason = detailModel.RESION;
            model.frequency = detailModel.FREQUENCY;
        }
 
    }else {
        
        for (HQc_codeModel *model in self.dataArray) {
            model.point = [model.SCORE doubleValue];
        }
 
    }

    self.model.chooseDepartTimes = 1;
    

    UIBarButtonItem *execbtn = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(didSaveButttonClicked:)];
    self.navigationItem.rightBarButtonItem = execbtn;
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentTextFieldDidEndediting:) name:UITextFieldTextDidChangeNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentTextViewDidediting:) name:UITextViewTextDidChangeNotification object:nil];

    
    //点击空白处回收键盘
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGestureRecognizer.cancelsTouchesInView = NO;
    //将触摸事件添加到当前view
    [self.view addGestureRecognizer:tapGestureRecognizer];

    //添加观察者 当键盘弹出时，tableview随之上移,不会遮挡
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}
//这一句不能去掉  否则点击屏幕的任何一个地方都会崩溃
- (void)keyboardHide:(UITapGestureRecognizer *)tap {
    [self.view endEditing:YES];
}
#pragma mark 键盘出现
-(void)keyboardWillShow:(NSNotification *)note
{
    CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.tableview.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0);
}
#pragma mark 键盘消失
-(void)keyboardWillHide:(NSNotification *)note
{
    self.tableview.contentInset = UIEdgeInsetsZero;
}

- (void)didSaveButttonClicked:(UIButton *)sender
{
    
    if (self.inspectModel.DEPT_ID.length == 0) {
        [HCommon popupErrorMessage:@"检查科室不能为空"];
        return;
    }
    CGFloat allPoint = 0;
    NSMutableArray *reasonArray = [NSMutableArray new];
    CGFloat ALLPoint = 0;
    

    for (int i = 0; i < self.bigArray.count; i ++) {
        
        Model_Hr_InspectDetail_New *detail_model = self.bigArray[i];
        
        detail_model.check_flag = 1;
        
        HQc_codeModel *model = self.dataArray[i];
        
        if (detail_model.RESION_ID.length == 0) {
            
            detail_model.RESION_ID = @"";
        }
        
        CGFloat point  =  [model.SCORE doubleValue] * [model.WEIGHTS doubleValue];
        
        allPoint += point;
        
        if (detail_model.RESION.length != 0) {
            
            [reasonArray addObject:detail_model.RESION];
            
        }
        if (model.SCORE.doubleValue < model.point && model.reason.length == 0 && model.SCORE.doubleValue >= 0) {
            
            [HCommon popupErrorMessage:[NSString stringWithFormat:@"请说明%@扣分原因",model.CODE_NAME]];
            return;
        }
        if (model.SCORE.doubleValue < 0) {
            [HCommon popupErrorMessage:[NSString stringWithFormat:@"分数不能为负数"]];
            return;
        }
        if (model.SCORE.doubleValue > model.point) {
            [HCommon popupErrorMessage:[NSString stringWithFormat:@"分数不能超过最大分值"]];
            return;
        }
        ALLPoint = allPoint;
    }
    if (allPoint >= [self.inspectModel.SCORE doubleValue]) {
        allPoint = [self.inspectModel.SCORE doubleValue];
    }else if (allPoint < 0)
    {
        allPoint = 0;
    }
//    CGFloat score = [self.model.SCORE doubleValue];
//    CGFloat point = score - allPoint;
    
    self.inspectModel.SCORE = [NSString stringWithFormat:@"%.2f",ALLPoint];
    self.inspectModel.UPLOAD_FLAG = 0;
    self.inspectModel.NURSE_ID = @"";
    self.inspectModel.NURSE_NAME = @"";
    self.inspectModel.ROUND_HOURS = @"";
    self.inspectModel.RESUL = [reasonArray componentsJoinedByString:@","];
    
    
    NSDictionary *department = @{@"department_id":self.inspectModel.DEPT_ID,@"department":self.departButton.titleLabel.text};
    [[TMCache sharedCache]setObject:department forKey:@"depart"];
    
    
    
    if (self.type ==  3) {
        
        if (self.isCheck == 1) {
            
        [[HQualitySave alloc]initWithUpdateFormData:self.inspectModel inspectDetailArray:self.bigArray];

        }else {
            
        [[HQualitySave alloc]initWithFormData:self.inspectModel inspectDetailArray:self.bigArray];

        }

    }else if (self.type == 4) {
        
        if (self.isCheck == 1) {
            
            [[HQualitySave alloc]initWithUpdateFormDataArray:self.bigArray];
            
        }else {
            
            [[HQualitySave alloc]initWithFormDatainspectDetailArray:self.bigArray];

        }

    }

    
    [HCommon popupSuccessMessage:@"保存成功"];

    NSArray *temArray = self.navigationController.viewControllers;
    
    NSNotification * notice = [NSNotification notificationWithName:@"shuaxin" object:nil userInfo:nil];
    [[NSNotificationCenter defaultCenter]postNotification:notice];
    

    
    for(UIViewController *temVC in temArray)
        
    {
        
        if ([temVC isKindOfClass:[HQualityDetailViewController class]])
            
        {
            self.getBlockInspect(self.inspectModel.DEPT_ID);
            [self.navigationController popToViewController:temVC animated:YES];
            
        }
    }
}

#pragma mark 给主表和明细表赋值
- (void)assigmentData
{
    self.inspectModel = [[Model_Hr_Ns_Inspect_New alloc]init];
    if (self.type == 3) {
        
    self.inspectModel.RESULT_ID = [super createPrimaryID];

    }else if (self.type == 4) {
        
    self.inspectModel.RESULT_ID = self.result_id;
        
    }
    for (int i = 0; i < self.dataArray.count; i++) {
        HQc_codeModel *model = self.dataArray[i];
        Model_Hr_InspectDetail_New *detailModel = [[Model_Hr_InspectDetail_New alloc]init];
        
        if (self.type == 3) {
            
            detailModel.RESULT_ID = [super createPrimaryID];
            
        }else if (self.type == 4) {
            
            detailModel.RESULT_ID = self.result_id;
            
        }

        detailModel.CODE_ID = model.CODE_ID;
        detailModel.par_id = model.PAR_ID;
        detailModel.SCORE = [model.SCORE floatValue];
        detailModel.RESION = @"";
        detailModel.UPLOAD_FLAG = 0;
        
        [self.bigArray addObject:detailModel];
    }
    //以后从menu中获取
    self.inspectModel.LEVEL_ID = self.level_id;
    self.inspectModel.TYPE_ID = self.model.TYPE_ID;
    self.inspectModel.REMARKS = @"";
    self.inspectModel.CODE_ID = self.model.CODE_ID;
    self.inspectModel.STANDARD_ID = self.model.STANDARD_ID;
    self.inspectModel.CHECKER_ID = [UserDefault getUserInfo].USER_ID;
    self.inspectModel.CHECKER_NAME = [UserDefault getUserInfo].USER_NAME;
    self.inspectModel.SCORE = self.model.SCORE ;
    self.inspectModel.INPUT_USER_ID = [UserDefault getUserInfo].USER_ID;
    self.inspectModel.INPUT_USER_NAME = [UserDefault getUserInfo].USER_NAME;
    self.inspectModel.INPUT_DATE = [super getNowDate];
    
}
- (UILabel *)dateTimeLabel
{
    if (!_dateTimeLabel) {
        _dateTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, 100, 30)];
        _dateTimeLabel.text = @"检查时间:";
        _dateTimeLabel.font = [UIFont systemFontOfSize:15];
        
    }
    return _dateTimeLabel;
}
- (UIButton *)dateTimeButton
{
    if (!_dateTimeButton) {
        _dateTimeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _dateTimeButton.frame = CGRectMake(self.dateTimeLabel.right + 10, 40,200, 30);
        _dateTimeButton.backgroundColor = [UIColor whiteColor];
        _dateTimeButton.layer.borderColor = [RGB(212,212,212) CGColor];
        _dateTimeButton.layer.borderWidth = 0.3f;
        _dateTimeButton.layer.masksToBounds = YES;
        _dateTimeButton.layer.cornerRadius = 5;
//        [_dateTimeButton setTitle:[super getNowDate] forState:UIControlStateNormal];
        [_dateTimeButton setTitle:[[TMCache sharedCache] objectForKey:@"checkDate"] forState:UIControlStateNormal];
        _dateTimeButton.titleLabel.textAlignment = NSTextAlignmentLeft;
        [_dateTimeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _dateTimeButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_dateTimeButton addTarget:self action:@selector(timeDate:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _dateTimeButton;
}

- (MPickerSelectionView *)pickerSelectionView {
    if (!_pickerSelectionView) {
        _pickerSelectionView = [[MPickerSelectionView alloc]init];
        _pickerSelectionView.delegate = self;
        
    }
    return _pickerSelectionView;
}
- (UIDatePicker *)datePicker {
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc]init];
        _datePicker.backgroundColor = [UIColor whiteColor];
        _datePicker.datePickerMode = UIDatePickerModeDateAndTime;
        _datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    }
    return _datePicker;
}
- (UITableView *)tableview
{
    if (!_tableview ) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight - NavBarHeight) style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        [_tableview registerClass:[HQCSpecificTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableview.tableFooterView = [[UIView alloc]init];
    }
    return _tableview;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HQCSpecificTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    HQc_codeModel *model = self.dataArray[indexPath.row];
    
    if ([model.KEYCODE_FLAG isEqualToString:@"1"]) {
        
        self.array = [[InitDbTable alloc]code_content:model.CODE_ID];
        
        self.itemArray = [[InitDbTable alloc]code_cotent_item:model.CODE_ID];
        
    }

    
    InitDbTableQuality *initDb = [[InitDbTableQuality alloc]init];
    
    NSMutableArray *reasonArray = [NSMutableArray new];
    
    reasonArray = [initDb getChooseReasonsWithCode_id:model.CODE_ID];

    if (reasonArray.count == 0) {
        
        cell.chooseReasonButton.hidden = YES;
        
        cell.reducePointTextView.width = ScreenWidth - 20;
        
    }else {
        cell.chooseReasonButton.tag = 200 + indexPath.row;
        
        [cell.chooseReasonButton addTarget:self action:@selector(chooseReasons:) forControlEvents:UIControlEventTouchUpInside];

    }
    
    if (self.isHasPC == 0) {
        
        cell.frequencyLabel.hidden = YES;
        
        cell.frequencyTextfield.hidden = YES;
    }
    
    cell.pointTextfield.tag = indexPath.row;
    
    cell.frequencyTextfield.tag = 100 + indexPath.row;

    cell.reducePointTextView.tag = indexPath.row;
    
    
    cell.titleLabel.text = [NSString stringWithFormat:@"%ld. %@",(long)indexPath.row + 1,model.CODE_NAME];
    
    cell.ExplainLabel.text= [NSString stringWithFormat:@"备注: %@",model.INSPECTION_METHOD?:@""] ;
    
    cell.pointTextfield.text = [NSString stringWithFormat:@"%@",model.SCORE?:@""];
    
    if (model.frequency == 0) {
        
        cell.frequencyTextfield.text  = @"";
        
    }else {
        
        cell.frequencyTextfield.text = [NSString stringWithFormat:@"%ld",(long)model.frequency]?:@"";

    }
    
    cell.reducePointTextView.text = [NSString stringWithFormat:@"%@",model.reason?:@""];
    
    [cell.button addTarget:self action:@selector(pushToNew:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.button.tag = 100 + indexPath.row;
    
    [cell.pointTextfield addTarget:self action:@selector(textFieldChanged2:) forControlEvents:UIControlEventEditingChanged];
    
    [cell.frequencyTextfield addTarget:self action:@selector(textFieldChanged1:) forControlEvents:UIControlEventEditingChanged];

    [cell setUpcell:cell array:self.array itemArray:self.itemArray flag:model.KEYCODE_FLAG block:^(NSInteger height ) {
        self.cellHeight = height ;
    }];
    return cell;
}


- (void)pushToNew:(UIButton *)sender
{
    
    
    DQFormViewController *vc = [[DQFormViewController alloc]initWithArray:self.array itemArray:self.itemArray result_id:self.inspectModel.RESULT_ID isSaved:0];
    
    HQc_codeModel *model = self.dataArray[sender.tag - 100];
    
    NSMutableArray *array = [NSMutableArray new];
    
    NSMutableArray *itemArray = [NSMutableArray new];
    
    if ([model.KEYCODE_FLAG isEqualToString:@"1"]) {
        
        array = [[InitDbTable alloc]code_content:model.CODE_ID];
        
        itemArray = [[InitDbTable alloc]code_cotent_item:model.CODE_ID];
        
    }
    vc.array = array;
    
    vc.itemArray = itemArray;


    InitDbTableQuality *initd = [[InitDbTableQuality alloc]init];
    
    
    NSMutableArray *Inspect = [initd GetInspextNewWithReslut_id:self.inspectModel.RESULT_ID code_id:model.CODE_ID];
    
    vc.editArray = Inspect;
    vc.title = @"输入关键指标";
    __weak HQCSpecificViewController *weakSelf = self;
    vc.getBlock = ^(NSString *result_id) {
        
        weakSelf.inspectModel.RESULT_ID = result_id;
    
    };

    [self.navigationController pushViewController:vc animated:YES];
}

- (void)textFieldChanged1:(UITextField *)textField
{
    HQc_codeModel *model = self.dataArray[textField.tag - 100 ];
    Model_Hr_InspectDetail_New *new_model = self.bigArray[textField.tag - 100];
    model.frequency = [textField.text integerValue];
    new_model.FREQUENCY = [textField.text integerValue];
}
- (void)textFieldChanged2:(UITextField *)textField{
    
    HQc_codeModel *model = self.dataArray[textField.tag];
    Model_Hr_InspectDetail_New *new_model = self.bigArray[textField.tag];
    model.SCORE = textField.text;
    new_model.SCORE = [textField.text floatValue];
}


- (void)contentTextViewDidediting:(NSNotification *)noti
{
    UITextView *textView = noti.object;
    HQc_codeModel *model = self.dataArray[textView.tag];
    Model_Hr_InspectDetail_New *new_model = self.bigArray[textView.tag];
    model.reason = textView.text?:@"";
    new_model.RESION = textView.text;
    new_model.RESION_ID = [NSString stringWithFormat:@"%@",@""];

}
//
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 230;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (void)chooseDepart:(UIButton *)sender
{
    HQCDepartViewController *HQCDVC = [[HQCDepartViewController alloc]initWithType:1 modelArray:nil standard_id:self.model.STANDARD_ID];
    UINavigationController *navHQCDVC = [[UINavigationController alloc]initWithRootViewController:HQCDVC];
    HQCDVC.title = @"选择";
    __weak HQCSpecificViewController *weakSelf = self;

    HQCDVC.getBlocks = ^(NSString *dept_name , NSString *dept_id) {
        weakSelf.model.departMent = dept_id;
        weakSelf.inspectModel.DEPT_ID = dept_name;
        weakSelf.model.chooseDepartTimes = 2;
        [weakSelf.tableview reloadData];
    };
    [self presentViewController:navHQCDVC animated:YES completion:NULL];
}

- (void)increase:(UIButton *)sender
{
    HQCDepartViewController *HQCDVC = [[HQCDepartViewController alloc]initWithType:2 modelArray:self.arrays standard_id:nil];
    UINavigationController *navHQCDVC = [[UINavigationController alloc]initWithRootViewController:HQCDVC];
    HQCDVC.title = @"选择";
    __weak HQCSpecificViewController *weakSelf = self;

    HQCDVC.getBlock = ^(NSArray *array) {
        weakSelf.arrays = (NSMutableArray *)array;
        for (Model_Pub_Employee *model in array) {
                if (![weakSelf.dept_nameArray containsObject:model.EMP_NAME]) {
                    [weakSelf.dept_nameArray addObject:model.EMP_NAME];
                }
                if (![weakSelf.dept_idArray containsObject:model.EMP_ID]) {
                    [weakSelf.dept_idArray addObject:model.EMP_ID];
                }
            }
        
        NSString *string = [weakSelf.dept_nameArray componentsJoinedByString:@","];
        NSString *idString = [weakSelf.dept_idArray componentsJoinedByString:@","];
        weakSelf.model.OthercheckPeople = string;
        weakSelf.inspectModel.OTHHER_CHAECK_NAME = string?:@"";
        weakSelf.inspectModel.OTHHER_CHAECK_ID = idString?:@"";
        [self.tableview reloadData];
    };
    [self presentViewController:navHQCDVC animated:YES completion:NULL];
}
- (void)check_people:(UIButton *)sender
{
    HQCDepartViewController *HQCDVC = [[HQCDepartViewController alloc]initWithType:3 modelArray:nil standard_id:nil];
    UINavigationController *navHQCDVC = [[UINavigationController alloc]initWithRootViewController:HQCDVC];
    HQCDVC.title = @"选择";
    __weak HQCSpecificViewController *weakSelf = self;

    HQCDVC.getBlock_nurse = ^(NSString *emp_name , NSString *emp_id) {
        weakSelf.model.checkPeople = emp_name;
        weakSelf.inspectModel.CHECKER_NAME = emp_name;
        weakSelf.inspectModel.CHECKER_ID = emp_id;
        [[TMCache sharedCache]setObject:emp_name forKey:@"CHECK_NAME"];
        [[TMCache sharedCache]setObject:emp_name forKey:@"CHECK_ID"];
        [weakSelf.tableview reloadData];
    };
    [self presentViewController:navHQCDVC animated:YES completion:NULL];

}

- (void)reduce:(UIButton *)sender
{
    if (self.arrays.count > 0) {
        [self.dept_nameArray removeObjectAtIndex:self.dept_nameArray.count - 1];
        [self.dept_idArray removeObjectAtIndex:self.dept_idArray.count - 1];
        NSString *string = [self.dept_nameArray componentsJoinedByString:@","];
        self.model.OthercheckPeople = string;
        [self.tableview reloadData];
    }
}

- (void)timeDate:(UIButton *)sender
{
    [self.pickerSelectionView present];
    
}
- (UIView *)viewWithMPickerSelectionView:(MPickerSelectionView *)pickerSelectionView {
    return self.datePicker;
}

- (void)confirmEventWithMPickerSelectionView:(MPickerSelectionView *)pickerSelectionView
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateString = [dateFormat stringFromDate:self.datePicker.date];
    self.inspectModel.CHECK_DATE = dateString;
    [[TMCache sharedCache]setObject:dateString forKey:@"checkDate"];

    [self.dateTimeButton setTitle:dateString forState:UIControlStateNormal];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    headView.backgroundColor = RGB(247, 247,247);
    UILabel *code_nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, ScreenWidth , 30)];
    code_nameLabel.text = self.model.CODE_NAME;
    code_nameLabel.font = [UIFont systemFontOfSize:17];
    
    
    UILabel *checkDepartLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,self.dateTimeLabel.bottom + 15, 100, 35)];
    checkDepartLabel.text = @"检查科室:";
    checkDepartLabel.font = [UIFont systemFontOfSize:15];
    
    
    self.departButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.departButton.frame = CGRectMake(checkDepartLabel.right + 10, self.dateTimeLabel.bottom + 15, ScreenWidth - checkDepartLabel.right - 20, 30);
    self.departButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.departButton.backgroundColor = [UIColor whiteColor];
    self.departButton.layer.borderColor = [RGB(212,212,212) CGColor];
    self.departButton.layer.borderWidth = 0.3f;

    NSString *departeMent = [[[TMCache sharedCache]objectForKey:@"depart"]objectForKey:@"department"];
    if (departeMent.length != 0 && self.model.chooseDepartTimes ==1) {
        [self.departButton setTitle:[[[TMCache sharedCache]objectForKey:@"depart"]objectForKey:@"department"] forState:UIControlStateNormal];
        self.inspectModel.DEPT_ID = [[[TMCache sharedCache]objectForKey:@"depart"]objectForKey:@"department_id"];

    }else {
        [self.departButton setTitle:self.model.departMent?:@"" forState:UIControlStateNormal];
    }

    self.departButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.departButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.departButton.tag = self.section + 100;
    [self.departButton addTarget:self action:@selector(chooseDepart:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *checkPeopleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.departButton.bottom + 15, 100, 35)];
    checkPeopleLabel.text = @"检查者";
    checkPeopleLabel.font = [UIFont systemFontOfSize:15];
    
    
    UILabel *OtherCheckPeopleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, checkPeopleLabel.bottom + 15, 100, 30)];
    OtherCheckPeopleLabel.text = @"其他检查者:";
    OtherCheckPeopleLabel.font = [UIFont systemFontOfSize:15];
    
    
    self.OtherCheckPeopleLabel = [[UILabel alloc]initWithFrame:CGRectMake(OtherCheckPeopleLabel.right + 10, checkPeopleLabel.bottom + 15, ScreenWidth - OtherCheckPeopleLabel.right - 100, 30)];
    self.OtherCheckPeopleLabel.backgroundColor = [UIColor whiteColor];
    self.OtherCheckPeopleLabel.layer.borderColor = [RGB(212,212,212) CGColor];
    self.OtherCheckPeopleLabel.layer.borderWidth = 0.3f;
    self.OtherCheckPeopleLabel.font = [UIFont systemFontOfSize:14];
    self.OtherCheckPeopleLabel.text = self.model.OthercheckPeople?:@"";
    self.OtherCheckPeopleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.checkPeopleButton = [UIButton buttonWithType: UIButtonTypeCustom];
    self.checkPeopleButton.frame = CGRectMake(checkPeopleLabel.right + 10, self.departButton.bottom + 15, 200, 30);
    self.checkPeopleButton.backgroundColor = [UIColor whiteColor];
    self.checkPeopleButton.layer.borderColor = [RGB(212,212,212) CGColor];
    self.checkPeopleButton.layer.borderWidth = 0.3f;
    self.checkPeopleButton.layer.masksToBounds = YES;
    [self.checkPeopleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.checkPeopleButton.layer.cornerRadius = 5;
    self.checkPeopleButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.checkPeopleButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.checkPeopleButton setTitle:self.model.checkPeople?:[UserDefault getUserInfo].USER_NAME forState:UIControlStateNormal];
    [self.checkPeopleButton addTarget:self action:@selector(check_people:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *increaseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    increaseButton.frame = CGRectMake(ScreenWidth - 100,checkPeopleLabel.bottom + 15, 40, 30);
    [increaseButton setTitle:@" + " forState:UIControlStateNormal];
    increaseButton.titleLabel.font = [UIFont systemFontOfSize:20];
    increaseButton.tag = self.section;
    [increaseButton addTarget:self action:@selector(increase:) forControlEvents:UIControlEventTouchUpInside];
    [increaseButton setTitleColor:kNavBlueColor forState:UIControlStateNormal];
    
    UIButton *reduceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [reduceButton setTitle:@" - " forState:UIControlStateNormal];
    [reduceButton setTitleColor:kNavBlueColor forState:UIControlStateNormal];
    reduceButton.titleLabel.font = [UIFont systemFontOfSize:20];
    reduceButton.tag = 50 + self.section;
    reduceButton.frame = CGRectMake(increaseButton.right, checkPeopleLabel.bottom + 15, 40, 30);
    [reduceButton addTarget:self action:@selector(reduce:) forControlEvents:UIControlEventTouchUpInside];

    

    
    [headView addSubview:code_nameLabel];
    [headView addSubview:self.dateTimeLabel];
    [headView addSubview:self.dateTimeButton];
    [headView addSubview:checkDepartLabel];
    [headView addSubview:self.departButton];
    [headView addSubview:checkPeopleLabel];
    [headView addSubview:OtherCheckPeopleLabel];
    [headView addSubview:self.checkPeopleButton];
    [headView addSubview:self.OtherCheckPeopleLabel];
    [headView addSubview:increaseButton];
    [headView addSubview:reduceButton];
    return headView;
}

- (void)chooseReasons:(UIButton *)sender

{
    HQc_codeModel *model = self.dataArray[sender.tag - 200];

    InitDbTableQuality *initDb = [[InitDbTableQuality alloc]init];

    NSMutableArray *reasonArray = [NSMutableArray new];

    reasonArray = [initDb getChooseReasonsWithCode_id:model.CODE_ID];

    if (reasonArray.count > 0) {
        
        HQChooseReasonViewController *HQCVC = [[HQChooseReasonViewController alloc]initWithModelArray:(NSArray *)reasonArray];
        
        HQCVC.getBlocks = ^(NSArray *array)
        {
            
            HQc_codeModel *model = self.dataArray[sender.tag - 200];
            
            Model_Hr_InspectDetail_New *new_model = self.bigArray[sender.tag - 200];
            
            model.reason = [[array valueForKey:@"reason_name"] componentsJoinedByString:@","]?:@"";
            
            new_model.RESION_ID = [[array valueForKey:@"reason_id"] componentsJoinedByString:@","]?:@"";
            new_model.RESION = [[array valueForKey:@"reason_name"] componentsJoinedByString:@","]?:@"";
            
            [self.tableview reloadData];
        
        };
        
        UINavigationController *navHQCDVC = [[UINavigationController alloc]initWithRootViewController:HQCVC];
        HQCVC.title = @"扣分原因";

        [self presentViewController:navHQCDVC animated:YES completion:NULL];



    }else {
        
        [self alert:@"可选扣分原因为空,请手动输入!"];
    }
    
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
